﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NamingOnLessonRepositry;
using NamingOnLessonEntity.Base;
using NamingOnLessonUtility;
using System.Data;

namespace NamingOnLesson.Extension
{
    /// <summary>
    /// 窗体公共基类
    /// </summary>
    /// <typeparam name="Tenttiy"></typeparam>
    public class PublicForm<Tenttiy> : Form where Tenttiy : BaseEntity, new()
    {
        public readonly RepositoryFactory<Tenttiy> repositryfactory = new RepositoryFactory<Tenttiy>();
        /// <summary>
        /// 加载表单窗体数据
        /// </summary>
        /// <param name="sysid">主键</param>
        /// <returns></returns>
        public virtual Tenttiy SetForm(string sysid)
        {
            Tenttiy entity = repositryfactory.Repository().FindEntity(sysid);
            return entity;
        }
        /// <summary>
        /// 保存表单窗体数据
        /// </summary>
        /// <param name="sysid">主键</param>
        /// <returns></returns>
        public virtual ResultInfo SubmitForm(Tenttiy entity, string sysid)
        {
            ResultInfo rs = new ResultInfo();
            try
            {
                int isOk = 0;
                string message = string.IsNullOrEmpty(sysid) ? "新增成功" : "编辑成功";
                if (!string.IsNullOrEmpty(sysid))
                {
                    entity.Modify(sysid);
                    isOk = repositryfactory.Repository().UpdateData(entity);
                }
                else
                {
                    entity.Create();
                    isOk = repositryfactory.Repository().InsertData(entity);
                }
                rs = new ResultInfo { success = true, code = 1, msg = message };
            }
            catch (Exception ex)
            {
                rs = new ResultInfo { success = false, code = -1, msg = "错误：" + ex.Message };
                throw;
            }
            return rs;
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="sysid">主键数组</param>
        /// <returns></returns>
        public virtual ResultInfo Delete(params string[] sysid)
        {
            ResultInfo rs = new ResultInfo();
            return rs;
        }
        /// <summary>
        /// 加载列表数据
        /// </summary>
        /// <param name="keyword">检索词</param>
        /// <returns></returns>
        public virtual DataTable GetDataTable(string keyword)
        {
            DataTable dt = new DataTable();
            return dt;
        }
    }
}
